﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Web.UI.HtmlControls;

using HotelWS;
using CE.Expedia;

public partial class travel_hotels_confirmation : System.Web.UI.Page
{
    HotelWS.HotelServicesImplService hws = new HotelWS.HotelServicesImplService();

    private string _apikey = "98pndgasdzzxpugjzqpxw6za";
    private int _minorrev = 13;
    private Int64 _cid = 381988;
    private Int64 _hotelid = 0;
    private string _roomtypeid = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.Culture = "en-US";

        if (!Page.IsPostBack)
        {

            if (Request.QueryString["bid"] != null && Request.QueryString["itid"] != null)
            {
                if (Request.QueryString["bid"] != "" && Request.QueryString["itid"] != "")
                {
                    if (GetItinerary(Request.QueryString["bid"].ToString(), Request.QueryString["itid"].ToString()))
                    {
                        GetHotelInfo(_hotelid, _roomtypeid);
                    }
                }
                else
                {
                    Response.Redirect("hotels-search.aspx");
                }

            }
            else
            {
                Response.Redirect("hotels-search.aspx");
            }

        }

    }


    protected void GetHotelInfo(Int64 hotelid, string roomtypeid)
    {
        HotelInformationRequest hotelreq = new HotelInformationRequest();
        hotelreq.apiKey = _apikey;
        hotelreq.cid = _cid;
        hotelreq.minorRev = _minorrev;
        hotelreq.minorRevSpecified = true;
        hotelreq.customerIpAddress = Request.UserHostAddress;
        hotelreq.customerUserAgent = Request.Browser.ToString();
        hotelreq.customerSessionId = "";
        hotelreq.currencyCode = "SGD";
        hotelreq.locale = LocaleType.en_US;
        hotelreq.localeSpecified = true;
        hotelreq.hotelId = hotelid;

        hotelInfoOption[] hotoption = new hotelInfoOption[5];
        hotoption[0] = hotelInfoOption.HOTEL_SUMMARY;
        hotoption[1] = hotelInfoOption.HOTEL_DETAILS;
        hotoption[2] = hotelInfoOption.ROOM_TYPES;
        hotoption[3] = hotelInfoOption.ROOM_AMENITIES;
        hotoption[4] = hotelInfoOption.PROPERTY_AMENITIES;

        HotelInformationResponse hotelresp = new HotelInformationResponse();
        hotelresp = hws.getInformation(hotelreq);

        if (hotelresp.HotelSummary != null && hotelresp.EanWsError == null)
        {

            if (hotelresp.PropertyAmenities != null)
            {
                lblAmenities.Text = "";

                foreach (PropertyAmenity proprow in hotelresp.PropertyAmenities.PropertyAmenity)
                {
                    lblAmenities.Text += proprow.amenity + " * ";
                }
            }

            if (hotelresp.HotelDetails != null)
            {
                lblNotification.Text = System.Web.HttpUtility.HtmlDecode(hotelresp.HotelDetails.roomInformation);

                lblCheckin.Text = lblCheckin.Text + " " + hotelresp.HotelDetails.checkInTime;
                lblCheckout.Text = lblCheckout.Text + " " + hotelresp.HotelDetails.checkOutTime;
            }


            if (hotelresp.RoomTypes.RoomType != null)
            {
                foreach (RoomType roomrow in hotelresp.RoomTypes.RoomType)
                {
                    if (roomrow.roomCode == roomtypeid)
                    {
                        if (roomrow.roomAmenities != null)
                        {
                            lblRoomAmenities.Text = "";

                            foreach (RoomAmenity roomam in roomrow.roomAmenities.RoomAmenity)
                            {
                                lblRoomAmenities.Text += roomam.amenity + " * ";
                            }

                        }
                        break;
                    }
                }

            }
            else
            { 
            }

            inputLat.Value = hotelresp.HotelSummary.latitude.ToString();
            inputLong.Value = hotelresp.HotelSummary.longitude.ToString();

        }
        else
        {
            lblMessage.Text = hotelresp.EanWsError.presentationMessage;
        }
    }

    protected bool GetItinerary(string bookingid, string itineraryid)
    {
        bool hasItinerary = false;
        ExpediaHotelBookingDB expdb = new ExpediaHotelBookingDB();
        DataTable dtBooking = new DataTable();
        dtBooking = expdb.GetBookingDetails(int.Parse(bookingid));

        if (dtBooking.Rows.Count > 0)
        {
            int noOfRooms = int.Parse(dtBooking.Rows[0]["NoOfRooms"].ToString());

            HotelItineraryRequest reqdetails = new HotelItineraryRequest();
            reqdetails.apiKey = _apikey;
            reqdetails.cid = _cid;
            reqdetails.minorRev = _minorrev;
            reqdetails.minorRevSpecified = true;
            reqdetails.customerIpAddress = Request.UserHostAddress;
            reqdetails.customerSessionId = dtBooking.Rows[0]["CustomerSessionId"].ToString(); //Session["customerSessionId"].ToString(); 
            reqdetails.customerUserAgent = Request.Browser.ToString();

            reqdetails.locale = LocaleType.en_US;
            reqdetails.localeSpecified = true;
            reqdetails.currencyCode = "SGD";
            reqdetails.itineraryId = int.Parse(itineraryid);
            reqdetails.itineraryIdSpecified = true;
            reqdetails.email = dtBooking.Rows[0]["Email"].ToString();
            reqdetails.lastName = dtBooking.Rows[0]["LastName"].ToString();
            reqdetails.affiliateConfirmationId = dtBooking.Rows[0]["HotelBookingGUID"].ToString();
            reqdetails.affiliateCustomerId = dtBooking.Rows[0]["HotelBookingId"].ToString();

            _hotelid = int.Parse(dtBooking.Rows[0]["HotelId"].ToString());

            string payathotel = "";
            string suppliertype = dtBooking.Rows[0]["SupplierType"].ToString();

            if (dtBooking.Rows[0]["info1"] != null)
            {
                payathotel = dtBooking.Rows[0]["info1"].ToString().Trim();
            }

            HotelItineraryResponse bookingdetails = new HotelItineraryResponse();
            bookingdetails = hws.getItinerary(reqdetails);

            if (bookingdetails.Itinerary != null && bookingdetails.EanWsError == null)
            {
                hasItinerary = true;
                lblMessage.Text = "";

                foreach (Itinerary irow in bookingdetails.Itinerary)
                {
                    lblItineraryNum.Text = irow.itineraryId.ToString();

                    lblBookingConfirmation.Text = "";
                    int iconfirm = 1;
                    float totalcharge = 0;
                    float taxes = 0;
                    bool hasHotelTax = false;
                    bool hasSalesTax = false;
                    float hoteltax = 0;
                    float salestax = 0;
                    float extrapersonfee = 0;
                    float servicefee = 0;

                    foreach (HotelConfirmation hconfirm in irow.HotelConfirmation)
                    {
                        string bookingstatus = "Confirmed"; //CF

                        if (hconfirm.status == "PS")
                        {
                            bookingstatus = "Pending";
                        }
                        else if (hconfirm.status == "UC")
                        {
                            bookingstatus = "Unconfirmed";
                        }
                        else if (hconfirm.status == "DT")
                        {
                            bookingstatus = "Deleted";
                        }

                        if (payathotel == "True" && suppliertype != "E")
                        {
                            lblConfirm.Text = "Your reservation is " + bookingstatus + ". Payment will be taken at the time of checkout by the hotel.";
                            lblNoCharge.Visible = true;
                        }

                        lblBookingConfirmation.Text += "Room " + iconfirm.ToString() + " - "
                            + hconfirm.ReservationGuest[0].firstName + " " + hconfirm.ReservationGuest[0].lastName + " - "
                            + hconfirm.confirmationNumber.ToString() + " - " + bookingstatus
                            + " <br/>";

                        _roomtypeid = hconfirm.roomType;

                        foreach (Hotel hoteldetails in hconfirm.Hotel)
                        {
                            lblHotelName.Text = hoteldetails.name;
                            lblHotelAddress.Text = hoteldetails.address1 + " " + hoteldetails.address2
                                        + " " + hoteldetails.city + ", " + hoteldetails.countryCode;

                            string _hotelrating = hoteldetails.hotelRating.ToString();

                            if (_hotelrating == "0")
                            { btnStars.CssClass = "rating-0"; }
                            else if (_hotelrating == "1" || _hotelrating == "1.0")
                            { btnStars.CssClass = "rating-1"; }
                            else if (_hotelrating == "1.5")
                            { btnStars.CssClass = "rating-15"; }
                            else if (_hotelrating == "2" || _hotelrating == "2.0")
                            { btnStars.CssClass = "rating-2"; }
                            else if (_hotelrating == "2.5")
                            { btnStars.CssClass = "rating-25"; }
                            else if (_hotelrating == "3" || _hotelrating == "3.0")
                            { btnStars.CssClass = "rating-3"; }
                            else if (_hotelrating == "3.5")
                            { btnStars.CssClass = "rating-35"; }
                            else if (_hotelrating == "4" || _hotelrating == "4.0")
                            { btnStars.CssClass = "rating-4"; }
                            else if (_hotelrating == "4.5")
                            { btnStars.CssClass = "rating-45"; }
                            else if (_hotelrating == "5" || _hotelrating == "5.0")
                            { btnStars.CssClass = "rating-5"; }

                            lblPropertyDetails.Text = "";
                            lblAmenities.Text = "";
                        }

                        lblCheckin.Text = Convert.ToDateTime(hconfirm.arrivalDate, System.Globalization.CultureInfo.InvariantCulture).ToString("MMM dd, yyyy");
                        lblCheckout.Text = Convert.ToDateTime(hconfirm.departureDate, System.Globalization.CultureInfo.InvariantCulture).ToString("MMM dd, yyyy");
                        lblNights.Text = hconfirm.nights.ToString();
                        lblGuest.Text = hconfirm.numberOfAdults.ToString() + " Adults";

                        if (hconfirm.numberOfChildren > 0)
                        {
                            lblGuest.Text += hconfirm.numberOfChildren.ToString() + " Child";
                        }

                        lblRoomType.Text = hconfirm.roomDescription;
                        lblRoomName.Text = hconfirm.roomDescription;
                        lblRooms.Text = noOfRooms.ToString();

                        string currency = hconfirm.RateInfos.RateInfo[0].ChargeableRateInfo.currencyCode;
                        float nightlyrate = 0;


                        if (suppliertype != "E")
                        {
                            divTotalCharges.Visible = false;
                            lblTaxDisclaimer.Text = "(Additional taxes and surcharges may be imposed by the hotel)";

                            lblPriceDisplayed.Text = "All prices are displayed in $ " + currency;

                            for (int i = 1; i <= noOfRooms; i++)
                            {
                                if (hconfirm.RateInfos.RateInfo[0].ChargeableRateInfo.maxNightlyRate != null)
                                {
                                    string maxnightrate = string.Format("{0:0,0.00}", hconfirm.RateInfos.RateInfo[0].ChargeableRateInfo.maxNightlyRate);
                                    lblPriceDetails.Text = "Room " + i.ToString() + " nightly rate* " + maxnightrate;
                                }
                            }

                        }

                        if (hconfirm.RateInfos.RateInfo[0].ChargeableRateInfo.currencyCode == "SGD")
                        {
                            lblPriceDetails.Text += "Room " + iconfirm.ToString() + " <br/>";
                            int idays = 0;
                            foreach (NightlyRate nightrow in hconfirm.RateInfos.RateInfo[0].ChargeableRateInfo.NightlyRatesPerRoom.NightlyRate)
                            {
                                //nightlyrate += nightrow.baseRate;
                                string nightrate = string.Format("{0:0,0.00}", nightrow.rate);

                                lblPriceDetails.Text += Convert.ToDateTime(hconfirm.arrivalDate, System.Globalization.CultureInfo.InvariantCulture).AddDays(idays).ToString("dd MMM yyyy") + " " + hconfirm.RateInfos.RateInfo[0].ChargeableRateInfo.currencyCode + " " +
                                        nightrate + "<br/>";

                                idays++;
                            }

                            //lblPriceDetails.Text += "Room " + iconfirm.ToString() + "   " +
                            //        currency + " " + string.Format("{0:0,0.00}", nightlyrate) + "<br/>";

                            //totalcharge += hconfirm.RateInfos.RateInfo[0].ChargeableRateInfo.total;
                            //lblTotalCharge.Text = currency + " " + string.Format("{0:0,0.00}", totalcharge.ToString());

                            if (hconfirm.RateInfos.RateInfo[0].ChargeableRateInfo.total <= 0)
                            {
                                totalcharge += hconfirm.RateInfos.RateInfo[0].ChargeableRateInfo.nightlyRateTotal;
                                lblTotalCharge.Text = currency + " " + string.Format("{0:0,0.00}", totalcharge);
                            }
                            else
                            {
                                totalcharge += hconfirm.RateInfos.RateInfo[0].ChargeableRateInfo.total;
                                lblTotalCharge.Text = currency + " " + string.Format("{0:0,0.00}", totalcharge);
                            }

                            if (hconfirm.RateInfos.RateInfo[0].ChargeableRateInfo.Surcharges != null)
                            {

                                foreach (Surcharge srow in hconfirm.RateInfos.RateInfo[0].ChargeableRateInfo.Surcharges.Surcharge)
                                {
                                    if (srow.type == SurchargeType.ExtraPersonFee)
                                    {
                                        extrapersonfee += srow.amount;
                                    }

                                    if (srow.type == SurchargeType.SalesTax)
                                    {
                                        hasSalesTax = true;
                                        salestax += srow.amount;
                                    }

                                    if (srow.type == SurchargeType.HotelOccupancyTax)
                                    {
                                        hasHotelTax = true;
                                        hoteltax += srow.amount;
                                    }

                                    if (srow.type == SurchargeType.ServiceFee)
                                    {
                                        servicefee += srow.amount;
                                    }

                                    if (srow.type == SurchargeType.TaxAndServiceFee)
                                    {
                                        taxes += srow.amount;
                                    }

                                }

                                if (extrapersonfee > 0)
                                {
                                    lblExtraPersonFee.Text = "Extra person fees" + " " + currency + " " + string.Format("{0:0,0.00}", extrapersonfee) + "<br/>";
                                }

                                if (servicefee > 0)
                                {
                                    lblServiceFee.Text = "Sevice Fee" + " " + currency + " " + string.Format("{0:0,0.00}", extrapersonfee) + "<br/>";
                                }

                                if (hasHotelTax && hasSalesTax)
                                {
                                    lblHotelTax.Text = "Hotel Occupancy and Sales Tax " + " " + currency + " " + (string.Format("{0:0,0.00}", hoteltax + salestax)) + "<br/>";
                                }
                                else
                                {
                                    if (hasHotelTax)
                                    {
                                        lblHotelTax.Text = "Hotel Occupancy Tax " + " " + currency + " " + (string.Format("{0:0,0.00}", hoteltax)) + "<br/>";
                                    }
                                    if (hasSalesTax)
                                    {
                                        lblSalesTax.Text += "Sales Tax " + " " + currency + " " + (string.Format("{0:0,0.00}", salestax)) + "<br/>";
                                    }
                                }

                                if (taxes > 0)
                                {
                                    lblPriceTax.Text = "Tax Recovery Charges and Service Fees " + currency + " " + string.Format("{0:0,0.00}", taxes) + "<br/>";

                                }
                            }

                        }
                        else   //converted
                        {
                            if (hconfirm.RateInfos.RateInfo[0].ConvertedRateInfo != null)
                            {
                                currency = hconfirm.RateInfos.RateInfo[0].ConvertedRateInfo.currencyCode;

                                lblPriceDetails.Text += "Room " + iconfirm.ToString() + " <br/>";
                                int idays = 0;

                                foreach (NightlyRate nightrow in hconfirm.RateInfos.RateInfo[0].ConvertedRateInfo.NightlyRatesPerRoom.NightlyRate)
                                {
                                    //nightlyrate += nightrow.baseRate;
                                    string nightrate = string.Format("{0:0,0.00}", nightrow.rate);

                                    lblPriceDetails.Text += Convert.ToDateTime(hconfirm.arrivalDate, System.Globalization.CultureInfo.InvariantCulture).AddDays(idays).ToString("dd MMM yyyy") + " " + hconfirm.RateInfos.RateInfo[0].ConvertedRateInfo.currencyCode + " " +
                                            nightrate + "<br/>";

                                    idays++;
                                }

                                if (hconfirm.RateInfos.RateInfo[0].ConvertedRateInfo.total <= 0)
                                {
                                    totalcharge += hconfirm.RateInfos.RateInfo[0].ConvertedRateInfo.nightlyRateTotal;
                                    lblTotalCharge.Text = currency + " " + string.Format("{0:0,0.00}", totalcharge);
                                }
                                else
                                {
                                    totalcharge += hconfirm.RateInfos.RateInfo[0].ConvertedRateInfo.total;
                                    lblTotalCharge.Text = currency + " " + string.Format("{0:0,0.00}", totalcharge);
                                }

                                if (hconfirm.RateInfos.RateInfo[0].ConvertedRateInfo.Surcharges != null)
                                {

                                    foreach (Surcharge srow in hconfirm.RateInfos.RateInfo[0].ConvertedRateInfo.Surcharges.Surcharge)
                                    {
                                        if (srow.type == SurchargeType.ExtraPersonFee)
                                        {
                                            extrapersonfee += srow.amount;
                                        }

                                        if (srow.type == SurchargeType.SalesTax)
                                        {
                                            hasSalesTax = true;
                                            salestax += srow.amount;
                                        }

                                        if (srow.type == SurchargeType.HotelOccupancyTax)
                                        {
                                            hasHotelTax = true;
                                            hoteltax += srow.amount;
                                        }

                                        if (srow.type == SurchargeType.ServiceFee)
                                        {
                                            servicefee += srow.amount;
                                        }

                                        if (srow.type == SurchargeType.TaxAndServiceFee)
                                        {
                                            taxes += srow.amount;
                                        }

                                    }

                                    if (extrapersonfee > 0)
                                    {
                                        lblExtraPersonFee.Text = "Extra person fees" + " " + currency + " " + string.Format("{0:0,0.00}", extrapersonfee) + "<br/>";
                                    }

                                    if (servicefee > 0)
                                    {
                                        lblServiceFee.Text = "Sevice Fee" + " " + currency + " " + string.Format("{0:0,0.00}", extrapersonfee) + "<br/>";
                                    }

                                    if (hasHotelTax && hasSalesTax)
                                    {
                                        lblHotelTax.Text = "Hotel Occupancy and Sales Tax " + " " + currency + " " + (string.Format("{0:0,0.00}", hoteltax + salestax)) + "<br/>";
                                    }
                                    else
                                    {
                                        if (hasHotelTax)
                                        {
                                            lblHotelTax.Text = "Hotel Occupancy Tax " + " " + currency + " " + (string.Format("{0:0,0.00}", hoteltax)) + "<br/>";
                                        }
                                        if (hasSalesTax)
                                        {
                                            lblSalesTax.Text += "Sales Tax " + " " + currency + " " + (string.Format("{0:0,0.00}", salestax)) + "<br/>";
                                        }
                                    }

                                    if (taxes > 0)
                                    {
                                        lblPriceTax.Text = "Tax Recovery Charges and Service Fees " + currency + " " + string.Format("{0:0,0.00}", taxes) + "<br/>";

                                    }
                                }
                            }
                        }


                        if (hconfirm.ReservationGuest != null)
                        {

                            lblGuests.Text += "Room " + iconfirm.ToString() + "<br/>";

                            if (hconfirm.numberOfChildren > 0)
                            {
                                lblGuests.Text += "Guests: " + hconfirm.ReservationGuest[0].firstName + " " + hconfirm.ReservationGuest[0].lastName + ", " + hconfirm.numberOfAdults.ToString() + " Adults";
                                lblGuests.Text += " , " + hconfirm.numberOfChildren.ToString() + " Child <br/>";
                            }
                            else
                            {
                                lblGuests.Text += "Guests: " + hconfirm.ReservationGuest[0].firstName + " " + hconfirm.ReservationGuest[0].lastName + ", " + hconfirm.numberOfAdults.ToString() + " Adults <br/>";
                            }

                        }

                        lblCancellation.Text = hconfirm.cancellationPolicy;

                        iconfirm++;
                    }

                    lblCardholder.Text = irow.Customer.firstName + " " + irow.Customer.lastName;
                    lblBillingAddress.Text = irow.Customer.CustomerAddresses[0].address1;

                    if (irow.Customer.CustomerAddresses[0].address2 != "")
                    {
                        lblBillingAddress.Text += " " + irow.Customer.CustomerAddresses[0].address2;
                    }

                    lblBillingAddress.Text += ", " + irow.Customer.CustomerAddresses[0].city;

                    if (irow.Customer.CustomerAddresses[0].stateProvinceCode != "")
                    {
                        lblBillingAddress.Text += " " + irow.Customer.CustomerAddresses[0].stateProvinceCode;
                    }

                    lblBillingAddress.Text += ", " + irow.Customer.CustomerAddresses[0].countryCode;
                    lblBillingAddress.Text += ", " + irow.Customer.CustomerAddresses[0].postalCode;

                    lblEmailAddress.Text = irow.Customer.email;
                    lblPhone.Text = irow.Customer.homePhone;
                }
            }
            else
            {

                lblMessage.Text = bookingdetails.EanWsError.presentationMessage; // +" <br/>" + bookingdetails.EanWsError.verboseMessage;
                lblConfirm.Visible = false;
            }



        }

        return hasItinerary;
    }
}
